package com.shixianjun.leetcode;

public class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    ListNode(int[] nums) {
        if (nums.length > 0) {
            ListNode p = this;
            ListNode head = this;
            this.val = nums[0];
            for (int i = 1; i < nums.length; i++) {
                ListNode node = new ListNode(nums[i]);
                if (p != null) {
                    p.next = node;
                }
                p = node;
            }
        }
    }

    @Override
    public String toString() {
        StringBuffer sb = new StringBuffer();
        ListNode curr = this;
        while (curr != null) {
            sb.append(curr.val);
            sb.append(curr.next == null ? "\n" : ", ");
            curr = curr.next;
        }
        return sb.toString();
    }
}
